<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="名称" prop="typeName">
                <el-input
                        v-model="queryParams.typeName"
                        placeholder="请输入传感器类型名称"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <!--      <el-form-item label="单位" prop="typeUnit">-->
            <!--        <el-input-->
            <!--          v-model="queryParams.typeUnit"-->
            <!--          placeholder="请输入单位"-->
            <!--          clearable-->
            <!--          size="small"-->
            <!--          @keyup.enter.native="handleQuery"-->
            <!--        />-->
            <!--      </el-form-item>-->
            <!--      <el-form-item label="权重" prop="weight">-->
            <!--        <el-input-->
            <!--          v-model="queryParams.weight"-->
            <!--          placeholder="请输入权重"-->
            <!--          clearable-->
            <!--          size="small"-->
            <!--          @keyup.enter.native="handleQuery"-->
            <!--        />-->
            <!--      </el-form-item>-->
            <!--      <el-form-item label="是否有负数" prop="hasNegative">-->
            <!--        <el-input-->
            <!--          v-model="queryParams.hasNegative"-->
            <!--          placeholder="请输入是否有负数"-->
            <!--          clearable-->
            <!--          size="small"-->
            <!--          @keyup.enter.native="handleQuery"-->
            <!--        />-->
            <!--      </el-form-item>-->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['business:sensorType:add']"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['business:sensorType:edit']"
                >修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['business:sensorType:remove']"
                >删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="info"
                        plain
                        icon="el-icon-sort"
                        size="mini"
                        @click="toggleExpandAll"
                >展开/折叠
                </el-button>
            </el-col>
            <!--      <el-col :span="1.5">-->
            <!--        <el-button-->
            <!--          type="warning"-->
            <!--          plain-->
            <!--          icon="el-icon-download"-->
            <!--          size="mini"-->
            <!--          @click="handleExport"-->
            <!--          v-hasPermi="['system:type:export']"-->
            <!--        >导出</el-button>-->
            <!--      </el-col>-->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table
                v-if="refreshTable"
                v-loading="loading"
                :data="typeList"
                row-key="id"
                :default-expand-all="isExpandAll"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
            <!--      <el-table-column type="selection" width="55" align="center"/>-->
            <!--      <el-table-column label="主键" align="center" prop="id"/>-->
            <el-table-column label="名称" align="center" prop="typeName"/>
            <el-table-column label="单位" align="center" prop="typeUnit"/>
            <el-table-column label="数值缩放倍数" align="center" prop="weight"/>
            <!--      <el-table-column label="是否有负数值" align="center" prop="hasNegative">-->
            <!--        <template slot-scope="scope">-->
            <!--          <el-tag :type="scope.row.hasNegative === '1' ? 'success' : 'danger'" disable-transitions>-->
            <!--            {{scope.row.hasNegative === '1' ? "是" : "否" }}-->
            <!--          </el-tag>-->
            <!--        </template>-->
            <!--      </el-table-column>-->
            <el-table-column label="寄存器个数" align="center" prop="registerSize"/>
            <el-table-column label="期望数值类型" align="center" prop="valueType"/>
            <el-table-column label="最小值" align="center" prop="minValue"/>
            <el-table-column label="最大值" align="center" prop="maxValue"/>
            <!--      <el-table-column label="${comment}" align="center" prop="remark" />-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['business:sensorType:edit']"
                    >修改
                    </el-button>
                    <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['business:sensorType:remove']"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--    <pagination-->
        <!--      v-show="total>0"-->
        <!--      :total="total"-->
        <!--      :page.sync="queryParams.pageNum"-->
        <!--      :limit.sync="queryParams.pageSize"-->
        <!--      @pagination="getList"-->
        <!--    />-->

        <!-- 添加或修改传感器类型对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>

            <el-form ref="form" :model="form" :rules="rules" label-width="90px" size="small">

                <el-form-item label="名称" prop="typeName">
                    <el-input v-model="form.typeName" placeholder="名称"/>
                </el-form-item>

                <el-form-item label="等级" prop="typeLevel">
                    <el-radio-group v-model="form.typeLevel">
                        <el-radio-button :label="1">一级</el-radio-button>
                        <el-radio-button :label="2">二级</el-radio-button>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="上级" prop="parentId" v-if="form.typeLevel === 2">
                    <el-select v-model="form.parentId"
                               placeholder="请选择上级目录"
                    >
                        <el-option
                                v-for="item in level1List"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"/>
                    </el-select>
                </el-form-item>

                <el-form-item label="单位" prop="typeUnit" v-if="form.typeLevel === 2">
                    <el-input v-model="form.typeUnit" placeholder="单位"/>
                </el-form-item>

                <el-form-item label="倍数" prop="weight" v-if="form.typeLevel === 2">
                    <el-select v-model="form.weight"
                               placeholder="数值缩放倍数"
                               clearable>
                        <el-option
                                v-for="item in weightList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"/>
                    </el-select>
                </el-form-item>

                <!--        <el-form-item label="是否有负数" prop="hasNegative">-->
                <!--          <el-radio-group v-model="form.hasNegative">-->
                <!--            <el-radio label="1">是</el-radio>-->
                <!--            <el-radio label="0">否</el-radio>-->
                <!--          </el-radio-group>-->
                <!--        </el-form-item>-->

                <el-form-item label="数值类型" prop="valueType" v-if="form.typeLevel === 2">
                    <el-select v-model="form.valueType"
                               placeholder="数值类型"
                               clearable>
                        <el-option
                                v-for="item in this.valueTypeList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"/>
                    </el-select>
                </el-form-item>

                <el-form-item label="位个数" prop="bitSize" v-if="form.typeLevel === 2">
                    <el-col :span="16">
                        <el-input v-model="form.bitSize"
                                  placeholder="请输入位个数"
                                  clearable
                                  type="number"
                        />
                    </el-col>
                </el-form-item>

                <el-form-item label="寄存器个数" prop="registerSize" v-if="form.typeLevel === 2">
                    <el-col :span="16">
                        <el-input v-model="form.registerSize"
                                  placeholder="请输入寄存器个数"
                                  clearable
                                  type="number"
                        />
                    </el-col>
                </el-form-item>

                <el-form-item label="最小值" prop="minValue" v-if="form.typeLevel === 2">
                    <el-input v-model="form.minValue" placeholder="请输入最小值"/>
                </el-form-item>

                <el-form-item label="最大值" prop="maxValue" v-if="form.typeLevel === 2">
                    <el-input v-model="form.maxValue" placeholder="请输入最大值"/>
                </el-form-item>

                <el-form-item label="排序" prop="orderNum">
                    <el-col :span="16">
                        <el-input v-model="form.orderNum"
                                  placeholder="排序"
                                  clearable
                                  type="number"
                        />
                    </el-col>
                </el-form-item>

            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
                <el-button size="small" @click="cancel">取 消</el-button>
            </div>

        </el-dialog>
    </div>
</template>

<script>
import {
    listSensorType,
    listTypeLevel1,
    getSensorType,
    delSensorType,
    addSensorType,
    updateSensorType
} from '@/api/system/sensorType'

export default {
    name: "sensorType",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 传感器类型表格数据
            typeList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                // pageNum: 1,
                // pageSize: 10,
                typeName: null,
                typeUnit: null,
                weight: null,
                hasNegative: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
            valueTypeList: [
                {value: 'short', label: 'short'},
                {value: 'int', label: 'int'},
                {value: 'float', label: 'float'}
            ],
            weightList: [
                {value: 1, label: '1'},
                {value: 10, label: '10'},
                {value: 100, label: '100'},
                {value: 1000, label: '1000'},
                {value: 10000, label: '10000'},
            ],
            typeLevelList: [
                {value: 1, label: '一级目录'},
                {value: 2, label: '二级类型'}
            ],
            level1List: [],
            isExpandAll: false,
            refreshTable: true
        };
    },
    created() {
        this.getList()
    },
    methods: {


        /** 展开/折叠操作 */
        toggleExpandAll() {
            this.refreshTable = false
            this.isExpandAll = !this.isExpandAll
            this.$nextTick(() => {
                this.refreshTable = true
            })
        },


        /** 查询传感器类型列表 */
        getList() {
            this.loading = true;
            listSensorType(this.queryParams).then(response => {

                this.typeList = this.handleTree(response.data, "id");
                // this.typeList = response.rows

                this.loading = false
            });
        },

        // tableCellStyle({row, column, rowIndex, columnIndex}) {
        //   if (row.parentId === 0) {
        //     return 'background:blue; color:white'
        //   }
        // },

        getLevel1List() {

            this.level1List = []

            listTypeLevel1().then(response => {

                for (let item of response.data) {
                    let temp = {}
                    temp['value'] = item.id
                    temp['label'] = item.typeName

                    this.level1List.push(temp)
                }

            })
        },


        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                typeName: null,
                typeUnit: null,
                weight: null,
                hasNegative: '0',
                registerSize: 1,
                valueType: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.getLevel1List()
            this.reset()
            this.open = true
            this.title = "添加传感器类型"
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.getLevel1List()
            this.reset()
            const id = row.id || this.ids
            getSensorType(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改传感器类型"
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateSensorType(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addSensorType(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除传感器类型编号为"' + ids + '"的数据项？').then(function () {
                return delSensorType(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/type/export', {
                ...this.queryParams
            }, `type_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
